package com.base.pig.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.pig.entity.SysDemo;
import com.base.pig.service.SysDemoService;
import com.base.pig.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  demo
 * </p>
 *
 * @author mag
 * @since 2022-03-15
 */
@RestController
@RequestMapping("/sys-demo")
public class SysDemoController {

    @Autowired
    public SysDemoService sysDemoService;


    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public R save(@RequestBody SysDemo sysDemo){
        boolean b = sysDemoService.save(sysDemo);
        if (b){
        return R.ok(b,"添加成功");
        }
        return R.failed(null,"添加失败");
        }

    @ApiOperation(value = "根据id删除")
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id){
        sysDemoService.removeById(id);
        return R.ok(null,"删除成功");
    }

    @ApiOperation(value = "条件查询")
    @PostMapping("/get")
    public R list(@RequestBody SysDemo sysDemo){
        List<SysDemo> sysDemoList = sysDemoService.list(new QueryWrapper<>(sysDemo));
        return R.ok(sysDemoList,"分页查询信息成功");
    }

    @ApiOperation(value = "列表（分页）")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public R list(@PathVariable("pageNum")Long pageNum, @PathVariable("pageSize")Long pageSize){
        IPage<SysDemo> page = sysDemoService.page(
                new Page<>(pageNum, pageSize),
                null);
        return R.ok(page,"分页查询信息成功");
    }

    @ApiOperation(value = "详情")
    @GetMapping("/get/{id}")
    public R get(@PathVariable("id") Long id){
        SysDemo sysDemo = sysDemoService.getById(id);
        return R.ok(sysDemo,"查询成功");
    }

    @ApiOperation(value = "根据id修改")
    @PostMapping("/update/{id}")
    public R update(@RequestBody SysDemo sysDemo){
        if(sysDemo.getId()!=null)
        {
            long count = sysDemoService.count(Wrappers.<SysDemo>query().lambda().eq(SysDemo::getId,sysDemo.getId()));
            if(count != 0) {
                 return R.ok( sysDemoService.updateById(sysDemo),"修改成功");
            }
            return R.failed(null,"未找到该数据");
        }
        return R.failed(null,"id不能为空");
    }


}
